#!/bin/sh

#Copyright (c) 2014 Luigi Tarenga <luigi.tarenga@gmail.com>
#Distributed under the terms of a MIT license.

#wrapper to pkill. useful for ESXi and OpenWRT

signal=TERM
while getopts "0fx" opt; do
   case "$opt" in
    0) signal=0 ;;
    f) : ;;
    x) anchor1="^"; anchor2="$" ;;
    ?) exit 1 ;;
   esac
done
eval pattern=\$$OPTIND

case $(uname) in
 VMkernel)
   pgrep -l -f "$anchor1$pattern$anchor2" | { found=1; while read pid args; do kill -$signal $pid; found=0; done; exit $found; }
 ;;
 *)
   pkill "$@"
 ;;
esac
